//
//  XXJADViewController.m
//  不得姐
//
//  Created by 许小军 on 16/9/2.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

#import "XXJADViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "XXJAdItem.h"
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>
#import "XXJTabBarController.h"
@interface XXJADViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchIamge;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;

@property (weak, nonatomic) IBOutlet UIView *ADview;
@property(strong,nonatomic)UIImageView * adImageView;
@property(strong,nonatomic)XXJAdItem * item;
@property(strong,nonatomic)NSTimer * timer;
@end

@implementation XXJADViewController

-(UIImageView *)adImageView
{
    if (_adImageView==nil) {
        //UIImageView * imageView=[[UIImageView alloc]init];
        _adImageView=[[UIImageView alloc]init];
        [self.ADview addSubview:_adImageView];
        UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [_adImageView addGestureRecognizer:tap];
        _adImageView.userInteractionEnabled=YES;
        
        
        
       // _adImageView=imageView;
    }
    return _adImageView;
}
-(void)tap
{
    NSURL * url=[NSURL URLWithString:_item.ori_curl];
    UIApplication * app=[UIApplication sharedApplication];
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setLaunchIamge];
    
    [self loadData];
    
    _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}
-(void)timeChange
{
    static int i=3;
    
    
    
    if (i==0) {
        [self jump:nil];
    }
    i--;
    [_jumpBtn setTitle:[NSString stringWithFormat:@"跳转（%ds）",i] forState:UIControlStateNormal];
}

- (IBAction)jump:(id)sender
{
   // XXJTabBarController * tabBarVc=[[XXJTabBarController alloc]init];
    //[UIApplication sharedApplication].keyWindow.rootViewController=tabBarVc;
    
    [_timer invalidate];
}

-(void)loadData
{
    AFHTTPSessionManager * mgr=[AFHTTPSessionManager manager];
    NSMutableDictionary * parameters=[NSMutableDictionary dictionary];
    parameters[@"code2"]=code2;
    
    [mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        //[responseObject writeToFile:@"/Users/duoduo/Desktop/adDDDDD.plist" atomically:YES];
        
        NSDictionary * adDict=[responseObject[@"ad"]lastObject];
        _item=[XXJAdItem mj_objectWithKeyValues:adDict];
        
        CGFloat h=[UIScreen mainScreen].bounds.size.width/_item.w * _item.h;
        self.adImageView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, h);
        
        
        [self.adImageView sd_setImageWithURL:[NSURL URLWithString:_item.w_picurl]];
        
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
       // NSLog(@"error:%@",error);
    }];
    
    
}

-(void)setLaunchIamge
{
    if ([UIScreen mainScreen].bounds.size.height==736) {
        self.launchIamge.image=[UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }
    else if ([UIScreen mainScreen].bounds.size.height==667)
    {
        self.launchIamge.image=[UIImage imageNamed:@"LaunchImage-800-667h"];
    }
    else if ([UIScreen mainScreen].bounds.size.height==568)
    {
        self.launchIamge.image=[UIImage imageNamed:@"LaunchImage-568h"];
    }
    else
        
    {
        self.launchIamge.image=[UIImage imageNamed:@"LaunchImage-700"];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
